﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Ext.Net;
using Mitc.Portal.Admin.Core.Entities;
using System.Collections;
using System.Collections.Generic;
namespace Mitc.Portal.Webs.Layouts.Quantri
{
    public partial class Admin : LayoutsPageBase
    {
        public static readonly string CacheKenhTin = "CacheKenhTin";
        protected override void OnInit(EventArgs e)
        {
            Cache.Remove(CacheKenhTin);
            base.OnInit(e);
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Panel4.Title = "Hệ thống quản trị kênh bài"+SPContext.Current.Web.CurrentUser;

                CachingData();

                GetDataNode(TreePanel2);
            }
        }

        private void GetDataNode(TreePanel catGrid)
        {
            var rotWeb = SPContext.Current.Site.RootWeb;
            var colle = new TreeNodeCollection();
            var listdata = (List<GetObject>)Cache[CacheKenhTin];
            if (listdata != null)
            {
                GetCollNode(listdata, out colle);
            }
            Ext.Net.TreeNode root = new Ext.Net.TreeNode();
            ConfigItemCollection collconfig = new ConfigItemCollection() { 
                             new ConfigItem(){ Name ="CatName",Mode=ParameterMode.Value , Value=rotWeb.Title},                            
                            
                               new ConfigItem(){ Name ="SiteId",Mode=ParameterMode.Value , Value= rotWeb.ID.ToString() }   
                            };
            root.CustomAttributes.AddRange(collconfig);
            root.Expanded = true;
            catGrid.RootVisible = false;
            catGrid.Root.Add(root);
            var firstnode = new TreeNode();
            firstnode.Icon = Icon.Folder;
            firstnode.Expanded = true;
            root.Nodes.Add(firstnode);
            firstnode.CustomAttributes.AddRange(collconfig);
            firstnode.Nodes.AddRange(colle);
        }
        /// <summary>
        /// Get Coll node for Grid
        /// </summary>
        /// <param name="rotweb"></param>
        /// <param name="coll"></param>
        public void GetCollNode(List<GetObject> obj, out TreeNodeCollection coll)
        {
            TreeNodeCollection treeColl = new TreeNodeCollection();

            foreach (GetObject w in obj)
            {
                if (!w.SiteName.ToLower().Equals("search"))
                {

                    var tree = new TreeNode();
                    ConfigItemCollection collconfig = new ConfigItemCollection() { 
                             new ConfigItem(){ Name ="CatName",Mode=ParameterMode.Value , Value=w.SiteName.ToString()},          
                              new ConfigItem(){ Name ="SiteId",Mode=ParameterMode.Value , Value=w.SiteId.ToString()},                     
                            };

                    tree.CustomAttributes.AddRange(collconfig);
                    treeColl.Add(tree);

                }
            }

            coll = treeColl;
        }
        private void CachingData()
        {
            var rotWeb = SPContext.Current.Site.RootWeb;
            var _lock = new object();
            List<GetObject> olist;
            olist=(List<GetObject>)Cache["CacheKenhTin"];
            if (olist == null)
            {
                lock (_lock)
                {
                    olist = GetListObject(rotWeb.GetSubwebsForCurrentUser());
                    Cache.Add(CacheKenhTin,olist,null,System.Web.Caching.Cache.NoAbsoluteExpiration,new TimeSpan(0,0,60),System.Web.Caching.CacheItemPriority.Default,null);

                }
            }
        }
        /// <summary>
        /// Get data to list object
        /// </summary>
        /// <param name="rotWeb"></param>
        /// <returns>List<GetObject></returns>
        private List<GetObject> GetListObject(SPWebCollection rotWeb)
        {
            var oli = new List<GetObject>();
            foreach(SPWeb w in rotWeb)
            {
                int countWeb = w.Webs.Count;
                if (countWeb > 0)
                {
                    GetListObject(w.Webs);
                }
                else {
                    var obj = new GetObject() { 
                        SiteName=w.Title,
                        SiteId=w.ID,
                        Description=w.Description,
                        Childweb=w.Webs
                    };
                    oli.Add(obj);
                }
            }
            return oli;
        }
    }
}
